package com.douban.app.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.douban.app.mapper.LikeMapper;
import com.douban.app.model.Ilike;
import com.douban.app.service.LikeService;
import com.github.yulichang.query.MPJQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class LikeServiceImpl implements LikeService {
    @Autowired
    LikeMapper likeMapper;


    @Override
    public Ilike add(JSONObject jsonObject, String id) {
        Ilike ilike = new Ilike();
        System.out.println(jsonObject);
        JSONObject data = JSONObject.parseObject(jsonObject.get("data").toString());
        System.out.println(data.get("cover").toString());
        ilike.setUserid(Integer.parseInt(id));
        ilike.setUrl(data.get("url").toString());
        ilike.setSource(data.get("source").toString());
        ilike.setName(data.get("name").toString());
        ilike.setArtist(data.get("artist").toString());
        ilike.setCover(data.get("cover").toString());
        ilike.setFavorited((boolean)data.get("favorited"));
        if (ilike.isFavorited()==false){
            QueryWrapper<Ilike> ilikeQueryWrapper = new QueryWrapper<>();
            ilikeQueryWrapper.eq("name",ilike.getName());
            likeMapper.delete(ilikeQueryWrapper);
            return null;
        }
        likeMapper.insert(ilike);
        return ilike;
    }

    @Override
    public List<Ilike> allLike(String id) {
        List<Ilike> ilikes = likeMapper.selectJoinList(Ilike.class,
                new MPJQueryWrapper<Ilike>()
                        .selectAll(Ilike.class)
                        .leftJoin("dbuser db on t.userid=db.id")
                        .eq("db.id", Integer.parseInt(id)));
        return ilikes;
    }
}
